#!/bin/bash
#
# The script creates a disk image using the dmgbuild utility and signs it.

set -e

# Defaults
app_name="Wireshark"
dmgbuild="@DMGBUILD_EXECUTABLE@"
version="@PROJECT_VERSION@"
log_version="@LOG_PROJECT_VERSION@"
app_settings_file="@CMAKE_BINARY_DIR@/packaging/macosx/wireshark-app.dmgbuild"
dsym_settings_file="@CMAKE_BINARY_DIR@/packaging/macosx/wireshark-dsym.dmgbuild"
architecture=""

# Help message
#----------------------------------------------------------
help()
{
echo -e "
Create a custom dmg file to distribute Wireshark

USAGE
	$0

OPTIONS
	-h,--help
		Display this help message.

Icons are positioned and the background image is set in wireshark-app.dmgbuild.in
and wireshark-dsym.dmgbuild.in.
"
}

if [ ! -x "$dmgbuild" ] ; then
	echo "Error: \"$dmgbuild\" not found."
	exit 1
fi

# Parse command line arguments
while [ "$1" != "" ]
do
	case $1 in
		-a|--app-name)
			shift 1
			app_name="$1"
			;;
		-h|--help)
			help
			exit 0 ;;
		*)
			echo "Invalid command line option"
			exit 2 ;;
	esac
	shift 1
done

if lipo "$app_name.app/Contents/MacOS/$app_name" -verify_arch arm64 ; then
	architecture="Arm 64"
elif lipo "$app_name.app/Contents/MacOS/$app_name" -verify_arch x86_64 ; then
	architecture="Intel 64"
else
	echo "Error: $app_name.app missing or has unknown architecture."
	lipo "$app_name.app/Contents/MacOS/$app_name" -detailed_info
	exit 1
fi

if [[ $app_name = Log* ]] ; then
	version=$log_version
	app_settings_file="@CMAKE_BINARY_DIR@/packaging/macosx/logray-app.dmgbuild"
	dsym_settings_file="@CMAKE_BINARY_DIR@/packaging/macosx/logray-dsym.dmgbuild"
fi

app_vol_name="$app_name ${version}"
app_img_name="$app_vol_name $architecture.dmg"

printf "\nCreating application disk image %s\n" "$app_img_name"

"$dmgbuild" \
	--no-hidpi \
	-s "$app_settings_file" \
	"$app_vol_name" \
	"$app_img_name" || exit 1

dsym_vol_name="$app_name dSYM ${version}"
dsym_img_name="$dsym_vol_name $architecture.dmg"

printf "\nCreating debugging symbols disk image %s\n" "$dsym_img_name"

"$dmgbuild" \
	--no-hidpi \
	-s "$dsym_settings_file" \
	"$dsym_vol_name" \
	"$dsym_img_name" || exit 1

printf "\nSigning disk images\n"

# TN2206, "Signing Disk Images"
if [ -n "$CODE_SIGN_IDENTITY" ] ; then
	echo -e "Signing $app_img_name and $dsym_img_name"
	codesign \
		--sign "Developer ID Application: $CODE_SIGN_IDENTITY" \
		--timestamp \
		--verbose \
		"$app_img_name" "$dsym_img_name"
fi

exit 0
